from django.contrib.auth.hashers import make_password
from rest_framework import serializers

from app01.models import User


class UserSer(serializers.ModelSerializer):
    class Meta:
        model = User
        fields = '__all__'

    def create(self, validated_data):
        # 调用django提供的密码加密方法
        validated_data['password'] = make_password(validated_data['password'])
        # #验证数据使密码进行加密
        instance = User.objects.create(**validated_data)
        return instance


class LoginSer(serializers.ModelSerializer):
    cate=serializers.SlugRelatedField(slug_field='name',read_only=True)
    class Meta:
        model = User
        fields = '__all__'
